package dfsketches.terrain_generator;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.MemoryImageSource;

import javax.swing.JFrame;

import dfsketches.visualizer_2d.WindowCloser;

public class Window extends JFrame {

	private static final long serialVersionUID = -1955162665659594417L;

	public Window(int[][] forDraw) {
		super();

		terrain = forDraw;
		size = terrain.length;
		bitmap = new int[size * size];
		
		setName("Plotter");
		setTitle("Terrain Generator");
		setSize(size + 15, size + 40);
		setVisible(true);
		
		image = createImage(new MemoryImageSource(size, size, bitmap, 0, size));
		paint(getGraphics());
		addWindowListener(new WindowCloser());
	}
	
	public void draw() {
		int t = 0;
		for (int i = 0; i < size; i++) {
			int[] terrainI = terrain[i];
			for (int j = 0; j < size; j++) {
				int tIJ = terrainI[j];
				if (tIJ < -250) {
					tIJ = -250 & 0x000000FF;
				} else if (tIJ < 0) {
					tIJ = (tIJ) & 0x000000FF;
				} else if (tIJ < 5) {
					tIJ = ((tIJ+150) * 0x00010100) & 0x00FFFF00;
				} else if (tIJ < 150) {
					tIJ = ((tIJ+50) * 0x00000100) & 0x0000FF00;
				} else if (tIJ < 250) {
					tIJ = (tIJ * 0x00010101) & 0x00FFFFFF;
				} else {
					tIJ = 250 * 0x00010101;
				}
				bitmap[t++] = tIJ | 0xFF000000;
			}
		}
		image = createImage(new MemoryImageSource(size, size, bitmap, 0, size));
		Graphics gr = getGraphics();
		paint(gr);
	}
	
	public void paint(Graphics g) {
		g.drawImage(image, 5, 30, this);
	}
	
	private Image image;
	private int size;
	private int[][] terrain;
	private int[] bitmap;
}
